package team.se.tms.controller.menu;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.constant.SessionConstant;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.MenuDetail;
import team.se.tms.dto.ParameterMap;
import team.se.tms.service.MenuInfoService;
import team.se.tms.util.Jurisdiction;
import team.se.tms.util.ResultUtil;

import javax.annotation.Resource;
import java.util.List;


/**
 * 菜单信息控制层
 *
 * @author winkyle
 * @date 2018/11/6
 */
@Slf4j
@Controller
@RequestMapping(value="/menu")
public class MenuInfoController extends BaseController{

    @Autowired
    private MenuInfoService menuInfoService;


    /**
     * 访问系统首页changeMenu：切换菜单参数
     * todo 加上权限，去掉注释并删掉下一行即可
     * @return String 页面地址
     * @create on 2018/11/23 by winkyle
     * @update on 2018/12/7 by johnny
     */
    @RequestMapping(value="/to_main/{menuType}")
    public ModelAndView toMain(@PathVariable("menuType") String menuType) throws Exception {
        log.debug(SecurityUtils.getSubject().getPrincipal().toString()+"进入了主页");
        List<MenuDetail> menuDetails = (List<MenuDetail>)Jurisdiction.getSession().getAttribute(menuType+SessionConstant.MENUS);
        if(menuDetails == null) {
//            menuDetails = Jurisdiction.getAllowableMenus(menuInfoService.listMenuDetails(menuType));
            menuDetails = menuInfoService.listMenuDetails(menuType);
            Jurisdiction.getSession().setAttribute(menuType+SessionConstant.MENUS,menuDetails);
        }
        ParameterMap parameterMap = getParameterMap();
        parameterMap.put("menus",menuDetails);
        ModelAndView modelAndView = getModelAndView();
        modelAndView.addObject("pageData",ResultUtil.success(parameterMap));
        modelAndView.setViewName("main/main");
        log.debug("菜单有{}条",menuDetails.size());
        log.debug("菜单是:{}",menuDetails.toString());
        return modelAndView;
    }

    /**
     * 进入tab标签
     *
     * @return String 页面地址
     * @create on 2018/11/23 by winkyle
     */
    @RequestMapping(value="/to_tab")
    public String toTab(){
        return "main/tab";
    }

    /**
     * 打开默认界面
     *
     * @return String 页面地址
     * @create on 2018/11/23 by winkyle
     */
    @RequestMapping(value="/to_default")
    public String toDefault(){
        return "default";
    }
}
